package com.shiliao.app.activity;

import android.app.AlarmManager;
import android.app.Notification;
import android.app.NotificationManager;
import android.app.PendingIntent;
import android.app.Service;
import android.content.Intent;
import android.os.IBinder;
import android.os.SystemClock;
import android.support.annotation.Nullable;
import android.util.Log;

import com.shiliao.app.R;
import com.shiliao.app.manager.NetworkManager;
import com.shiliao.app.manager.YMUserManager;
import com.shiliao.app.utils.YMConfig;
import com.shiliao.app.utils.YMServerUtil;
import com.yimei.framework.network.ICallBack;
import com.yimei.framework.util.LogUtil;
import com.yimei.framework.util.ParseHelper;

import java.util.HashMap;
import java.util.Map;
import java.util.Timer;

/**
 * Created by Daniellu on 2018/1/12.
 */

public class AlarmService extends Service {

    static Timer timer = null;
    static String userId = "";

    @Nullable
    @Override
    public IBinder onBind(Intent intent) {
        return null;
    }

    @Override
    public void onCreate() {
        super.onCreate();
    }
    // 添加通知
    //public static void dailyNotification(Context mainContext) {
    //    Intent intent = new Intent(mainContext,
    //            AlarmService.class);
    //    mainContext.startService(intent);
    //}

    @Override
    public int onStartCommand(final Intent intent, int flags, int startId) {

        long period;

        //userId=intent.getStringExtra("user_id");
        userId = YMUserManager.getUserId(this);
        Log.w("For debug", "===========onStartCommand=======");
        if (YMConfig.YM_IS_RELEASE)
            period = 24 * 60 * 60 * 1000; // 正式运行时，24小时一个周期
        else
            period = 5 * 60 * 1000; // 5分钟一个周期

        requestReminder(userId);

        AlarmManager manager = (AlarmManager) getSystemService(ALARM_SERVICE);
        Intent i = new Intent(this, AlarmReceiver.class);
        i.setAction("YiMei_CLOCK");
        PendingIntent pi = PendingIntent.getBroadcast(this, 0, i, 0);
        manager.set(AlarmManager.ELAPSED_REALTIME_WAKEUP, SystemClock.elapsedRealtime() + period, pi);
        return START_STICKY;
    }

    public void requestReminder(String userId) {
        //Log.w("MyService", "===========requestReminder=======");

        Map map = new HashMap();
        map.put("user_id", userId);
        try {
            NetworkManager.loadRequsetByRaw(YMServerUtil.ACTION_NOTICE_CONTEN, map, new ICallBack() {
                @Override
                public void onSuccess(int statusCode, Object result) {
                    //final Bitmap largeIcon = ((BitmapDrawable) getResources().getDrawable(R.drawable.logo)).getBitmap();
                    //Log.w("MyService", "===========onSuccess=======");
                    String isNotice = ParseHelper.getString(result.toString(), "has_notice");
                    String noticeTitle = ParseHelper.getString(result.toString(), "notice");
                    if (isNotice.equals("yes")) {
                        NotificationManager mn = (NotificationManager) AlarmService.this
                                .getSystemService(NOTIFICATION_SERVICE);
                        Notification.Builder builder = new Notification.Builder(
                                AlarmService.this);

                        Intent notificationIntent = new Intent(AlarmService.this,
                                MainActivity.class);// 点击跳转位置
                        PendingIntent contentIntent = PendingIntent.getActivity(
                                AlarmService.this, 0, notificationIntent, 0);
                        builder.setContentIntent(contentIntent);
                        builder.setSmallIcon(R.mipmap.ic_launcher);// 设置图标
                        //builder.setLargeIcon(largeIcon);
                        builder.setContentTitle("日常提醒");// 设置通知的标题
//                            builder.setContentText("Testing"); // 设置通知的内容
                        builder.setContentText(noticeTitle); // 设置通知的内容

                        //builder.setWhen(System.currentTimeMillis());// 设置通知来到的时间
                        //builder.setAutoCancel(true); //自己维护通知的消失
                        builder.setTicker("有医美知识提醒");// 第一次提示消失的时候显示在通知栏上的
                        //builder.setPriority(Notification.PRIORITY_DEFAULT); //设置该通知优先级
                        //builder.setOngoing(true);
                        //builder.setNumber(1);
                        //  builder.setDefaults(Notification.DEFAULT_ALL);
                        builder.setVibrate(new long[]{0, 300, 500, 700});
                        Notification notification = builder.build();
                        //notification.flags = Notification.FLAG_NO_CLEAR;  //只有全部清除时，Notification才会清除
                        mn.notify(1, notification);

                    }
                    LogUtil.e("yzq", result + "=====");
                }

                @Override
                public void onFail(int statusCode, Object result) {
                    LogUtil.e("yzq", "getnoticecontent failed!");
                    return;
                }

            });
        } catch (Exception e) {
            e.printStackTrace();
        }


    }

    @Override
    public void onDestroy() {
        super.onDestroy();
    }
}
